﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;

namespace YouHP.Pages.Ajax
{
    public class LogCommentReplyAdd : YouHP.Pages.BasePage
    {

        protected LogTopicInfo LogData;
        protected LogCommentInfo LogCommentData;
 
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            int lid = RequestTools.GetQueryInt("lid", -1);
            int logcommentid = RequestTools.GetQueryInt("parentId", -1);
            LogData = LogTopicBLL.Instance.GetLogTopicInfo(lid);
            if (LogData.Id <= 0) return;
            LogCommentData = LogCommentBLL.Instance.GetLogCommentInfo(logcommentid);
            if(LogCommentData.Id<=0) return;


            LogCommentInfo info = ValidateForm();
            if (info == null) return;


            //执行添加
            bool succ = LogCommentBLL.Instance.AddLogComment(info);
            if (succ)
            {
                //生成返回XML
                StringBuilder sb = new StringBuilder();
                sb.Append("<div class=\"reply_item\">");
                sb.Append("<div style=\"float:right;\"><span>" + info.PostTime.ToString("yyyy-MM-dd hh:mm") + "</span></div>");
                sb.Append("<div><img alt=\"头像\" src=\"" + UserBLL.Instance.GetUserInfo(info.PostUserId).Avatar + "\" width=\"25px\" height=\"25px\" />");
                TempUrl = "/Profile/Index.aspx?uid=" + info.PostUserId;
                sb.Append("&nbsp;&nbsp;<a href=\"" + this.GetUrl(TempUrl) + "\">" + info.PostUserName + "</a><span>的回复</span>：");
                sb.Append("<div style=\"margin-left:40px;\">" + info.Body + "</div></div></div>");

                ResponseXML(sb);

            }
        }

        LogCommentInfo ValidateForm()
        {
            LogCommentInfo info = new LogCommentInfo();
            info.LogTopicId = LogData.Id;
            info.UpdateTime = DateTime.Now;
            info.PostTime = DateTime.Now;
            info.PostUserId = UserData.Id;
            info.PostUserName = UserData.UserName;
            info.ParentId = LogCommentData.Id;
            info.Body = RequestTools.GetQueryString("reply").Trim();
            if (UserData.Id == LogData.HostUserId)
            {
                info.ToUserId = LogCommentData.PostUserId;
            }
            else
            {
                info.ToUserId = LogData.HostUserId;
            }
            return info;
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}

